
import { Context } from 'koa';
import { validateDto } from '@/utils/validateDto';
import { CreateFieldInfoDto, UpdateFieldInfoDto, QueryFieldInfoDto, DeleteFieldInfoDto, SelectOneFieldInfoDto, SortFieldInfoDto, CreateFieldInfoListDto } from '@/dto/fieldInfo.dto';
import fieldInfoService from '@/services/fieldInfo.service';

class FieldInfoController {
    async selectList(ctx: Context) {

        const reqData = ctx.request.body as QueryFieldInfoDto;
        const errors = await validateDto(QueryFieldInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        let resData = await fieldInfoService.selectList(reqData)
        ctx.body = {
            code: 200,
            message: "列表查询成功",
            data: resData
        };
    }
    async selectOne(ctx: Context) {
        const reqData = ctx.request.body as SelectOneFieldInfoDto;
        let resData = await fieldInfoService.selectOne(reqData)


        ctx.body = {
            code: 200,
            message: "详情查询成功",
            data: resData,

        };
    }

    async add(ctx: Context) {
        const userId = ctx.state.userId as number;
        const reqData = ctx.request.body as CreateFieldInfoDto;
        //reqData.userId = reqData.userId || userId; //暂时注释
        const errors = await validateDto(CreateFieldInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await fieldInfoService.add(reqData);

        ctx.body = {
            code: 200,
            message: "添加成功",
            data: { id: resData.id },
        };

    }

    async update(ctx: Context) {
        const reqData = <UpdateFieldInfoDto>ctx.request.body
        const errors = await validateDto(UpdateFieldInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await fieldInfoService.update(reqData);


        ctx.body = {
            code: 200,
            message: "更新成功",
            data: {},
        };

    }
    async delete(ctx: Context) {
        const reqData = ctx.request.body as DeleteFieldInfoDto;
        const errors = await validateDto(DeleteFieldInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await fieldInfoService.delete(reqData);

        ctx.body = {
            code: 200,
            message: "删除成功",
            data: {},
        };

    }
    async sort(ctx: Context) {
        const reqData = ctx.request.body as SortFieldInfoDto;
        const errors = await validateDto(SortFieldInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await fieldInfoService.sort(reqData);

        ctx.body = {
            code: 200,
            message: "排序成功",
            data: {},
        };

    }
    async addBatch(ctx: Context) {
        const reqData = ctx.request.body as CreateFieldInfoListDto;
        const errors = await validateDto(CreateFieldInfoListDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await fieldInfoService.addBatch(reqData);


        ctx.body = {
            code: 200,
            message: "提交成功",
            data: {},
        };

    }
    async selectByParentId(ctx: Context) {
        const p = ctx.request.body as {parentId:number};
        

        const resData = await fieldInfoService.selectByParentId(p.parentId);


        ctx.body = {
            code: 200,
            message: "提交成功",
            data: resData,
        };

    }

}

export default new FieldInfoController();

